package com.sogoodsoft.core.service;

import java.io.Serializable;
import java.util.List;

import com.sogoodsoft.core.dao.EntityDao;
import com.sogoodsoft.core.support.Page;


/**
 * 业务层的基类
 */
public abstract class BaseService<T, PK extends Serializable> implements EntityService<T, PK> {

	protected abstract EntityDao<T, PK> getDao();

	/**
	 * 创建对象,子类必须实现
	 */
	@Override
	public void save(T entity) {
		getDao().save(entity);
	}

	/**
	 * 更新对象,子类必须实现
	 */
	@Override
	public T update(T entity) {
		return getDao().update(entity);
	}

	/**
	 * 删除对象,子类必须实现
	 */
	@Override
	public void delete(PK id) {
		getDao().delete(id);
	}

	/**
	 * 根据主键id查询对象,子类必须实现
	 */
	@Override
	public T load(PK id) {
		return getDao().load(id);
	}

	/**
	 * 查询所有对象列表,子类必须实现
	 */
	@Override
	public List<T> find() {
		return getDao().find();
	}

	@Override
	public List<T> find(final T entity) {
		return getDao().find(entity);
	}

	@Override
	public List<T> find(final T entity, final String orderBy, final boolean isAsc) {
		return getDao().find(entity, orderBy, isAsc);
	}

	@Override
	public List<T> find(final String queryString, final Object... values) {
		return getDao().find(queryString, values);
	}

	/**
	 * 根据输入条件进行分页查询,并可以判断有效性,子类必须实现
	 */
	@Override
	public Page findPaged(T entity, int pageNo, int pageSize) {
		return getDao().findPaged(entity, pageNo, pageSize);
	}
	
	
	@Override
	public String repeatValidation(String validateId,String validateValue,String validateError,String entity,String id){
		return "{jsonValidateReturn:['"+validateId+"','"+validateError+"','"+getDao().repeatValidation(entity, validateId, validateValue, id)+"']}";
	}

    @Override
    public Page findPaged(T entity, int pageNo, int pageSize, String orderBy, boolean isAsc) {
        return getDao().findPaged(entity, pageNo, pageSize, orderBy, isAsc);
    }

    @Override
    public Page findPaged(String queryString, int pageNo, int pageSize, Object... values) {
        return getDao().findPaged(queryString, pageNo, pageSize, values);
    }
    
}
